package server;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import common.SpinBox;

public class SpinBoxServer extends AbsServer {
	private SpinBox box = new SpinBox();

	public SpinBoxServer(int port) {
		super(port);
	}

	public SpinBox getSpinBox() {
		return box;
	}

	@Override
	public boolean start() {
		if(!super.start()) return false;
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					while (true) {
						Thread.sleep(50);
						box.addDegree(1.0);
					}
				} catch (InterruptedException e) {
				}
			}
		}).start();
		return true;
	}

	@Override
	public void replyAction(DatagramSocket socket, DatagramPacket pkt)
			throws Exception {
		pkt.setData(Double.toString(box.getDegree()).getBytes());
		// pkt.setData(clock.getTimeString().getBytes());
		socket.send(pkt);
	}

}
